package com.zg.autoflow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zg.autoflow.req.AutoflowInherentFlowAddReq;
import com.zg.autoflow.req.AutoflowInherentFlowUpdateReq;
import com.zg.autoflow.service.AutoflowInherentFlowService;
import com.zg.common.core.A;
import com.zg.common.core.dao.Inherent.AutoflowInherentFlow;
import com.zg.common.core.dao.Inherent.dao.AutoflowInherentFlowDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author zg
*/
@Service
@RequiredArgsConstructor
public class AutoflowInherentFlowServiceImpl implements AutoflowInherentFlowService {

    private final AutoflowInherentFlowDao autoflowInherentFlowDao;

    void checkAdd(AutoflowInherentFlowAddReq param) {
        //BusinessException.happen("这里是提示信息");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutoflowInherentFlow add(AutoflowInherentFlowAddReq param) {
        //校验逻辑
        checkAdd(param);
        //创建对象
        AutoflowInherentFlow obj = new AutoflowInherentFlow();
        //属性处理
        BeanUtil.copyProperties(param, obj);
        StringBuffer strBuff = new StringBuffer();
        obj.setSearchStr(strBuff.toString());
        //保存对象
        autoflowInherentFlowDao.save(obj);
        return obj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> idList) {
        autoflowInherentFlowDao.removeBatchByIds(idList);
    }

    void checkUpdate(AutoflowInherentFlowUpdateReq param) {
        //BusinessException.happen("这里是提示信息");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutoflowInherentFlow update(AutoflowInherentFlowUpdateReq param) {
        //查询对象
        AutoflowInherentFlow oldObj = autoflowInherentFlowDao.getById(param.getId());
        A.isInDatabase(oldObj, "要更新的对象");
        //校验逻辑
        checkUpdate(param);
        //属性处理
        BeanUtil.copyProperties(param, oldObj);
        StringBuffer strBuff = new StringBuffer();
        oldObj.setSearchStr(strBuff.toString());
        //更新对象
        autoflowInherentFlowDao.updateById(oldObj);
        return oldObj;
    }


}
